package com.google.android.apps.wakemewhere.ui;

import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.google.android.apps.wakemewhere.Alarm;
import com.google.android.apps.wakemewhere.AlarmArbiter;
import com.google.android.apps.wakemewhere.AlarmContentObserver;
import com.google.android.apps.wakemewhere.AlarmListAdapter;
import com.google.android.apps.wakemewhere.Alarms;
import com.google.android.apps.wakemewhere.IntentExtras;
import com.google.android.apps.wakemewhere.ProximityAlertAlarmArbiter;
import com.google.android.apps.wakemewhere.R;

public class AlarmListActivity extends ListActivity {

	private LocationManager locationManager;

	private AlarmArbiter arbiter = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		setupList();
		setupAddAlarmButton();

		if (arbiter == null) {
			arbiter = new ProximityAlertAlarmArbiter(this,
					(LocationManager) (getApplicationContext()
							.getSystemService(LOCATION_SERVICE)));
			
			
			AlarmContentObserver alarmObserver = new AlarmContentObserver(
					getApplicationContext(), arbiter);
			
			getApplicationContext().getContentResolver()
					.registerContentObserver(Alarms.CONTENT_URI, true,
							alarmObserver);
			
			Intent alertIntent = new Intent(getApplicationContext(),
					AlarmFireActivity.class);
			alertIntent.setAction(ProximityAlertAlarmArbiter.PROXIMITY_ALERT);
			alarmObserver.onChange(true);
		}
	}

	private void setupAddAlarmButton() {
		View addAlarmButton = findViewById(R.id.add_alarm);
		addAlarmButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				addAlarm();
			}
		});
		addAlarmButton
				.setOnFocusChangeListener(new View.OnFocusChangeListener() {
					public void onFocusChange(View v, boolean hasFocus) {
						v.setSelected(hasFocus);
					}
				});
	}

	private void setupList() {
		Cursor cursor = getContentResolver().query(Alarms.CONTENT_URI, null,
				null, null, null);
		startManagingCursor(cursor);

		setListAdapter(new AlarmListAdapter(this, cursor));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.alarm, menu);
		return true;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent(this, SetAlarmActivity.class);
		intent.putExtra(IntentExtras.ALARM_ID, id);
		startActivity(intent);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.add_alarm:
			addAlarm();
			return true;
		case R.id.test:
			addTestAlarm("Alarm1", 20000000, 20000000);
			addTestAlarm("Alarm2", 30000000, 30000000);
			addTestAlarm("Alarm3", 40000000, 40000000);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void addTestAlarm(String label, long latitude, long longitude) {
		Alarm alarm = new Alarm(1, true, false, true, "My Alarm", latitude, longitude);
		ContentValues values = new ContentValues();
		alarm.writeOnto(values);
		getContentResolver().insert(Alarms.CONTENT_URI, values);
	}

	private void addAlarm() {
		Intent intent = new Intent(this, LocationPickerActivity.class);
		intent.putExtra(IntentExtras.ALARM_ID, -1L);
		startActivity(intent);
	}
}